package service

import (
	"Graphite/database"
	"Graphite/model"
	"Graphite/util"
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"golang.org/x/crypto/bcrypt"
	"log"
	"math/rand"
)

//用户验证

func Login(c *gin.Context, user model.Login) {
	reqIP := c.ClientIP()
	if reqIP == "::1" {
		reqIP = "127.0.0.1"
	}
	//fmt.Println(reqIP)
	collection := database.DB.Collection("user")
	var one model.User
	//通过account从数据库中把该用户信息拿出来
	err := collection.FindOne(context.Background(), bson.M{"account": user.Account}).Decode(&one)
	if err != nil {
		util.Return(c,200, util.NOT_EXIST, err.Error())
		return
	} else {
		//密码加密进行比较
		err = bcrypt.CompareHashAndPassword([]byte(one.Password), []byte(user.Password+one.Salt))
		if err != nil {
			fmt.Println("Login err:", err)
			util.Return(c,200, util.LOGIN_FAIL, err.Error())
		} else {
			token, err := util.GenToken(one,reqIP)
			if err != nil {
				util.Return(c,200, util.SYS_BUSY, err.Error())
				return
			} else {
				util.Return(c,200, util.SUCCESS, token)
			}
		}
	}
}

//插入一条数据

func UserInsert(c *gin.Context, user model.User) {
	collection := database.DB.Collection("user")
	var one model.User
	//随机生成一个账号
	//for  {
	//	account:=fmt.Sprintf("%d",rand.Int31())
	//	err := collection.FindOne(context.Background(), bson.M{"account": account}).Decode(&one)
	//	if err!=nil {
	//		user.Account = account
	//		break
	//	}
	//}

	//判断账号是否注册
	err := collection.FindOne(context.Background(), bson.M{"account": user.Account}).Decode(&one)
	if err == nil {
		util.Return(c,200, util.HAS_EXIST, err)
		return
	} else {
		salt := fmt.Sprintf("%d", rand.Int31())
		hash, err := bcrypt.GenerateFromPassword([]byte(user.Password+salt), bcrypt.DefaultCost) //加密处理
		encodePWD := string(hash)
		user.Password = encodePWD
		user.Salt = salt
		insertOneResult, err := collection.InsertOne(context.Background(), user)
		if err != nil {
			log.Println(err)
		}
		log.Println("collection.InsertOne: ", insertOneResult.InsertedID)
		//util.Return(c,util.SUCCESS,user.Account)
		util.Return(c,200, util.SUCCESS, insertOneResult)
		return
	}
}
func UserUpdate(c *gin.Context,user model.User)  {
	switch user.Role {
	case "0":
		 user.Authority,_= primitive.ObjectIDFromHex("60a3702fee35a58c9f6dc6b3")
		break
	case "1":
		user.Authority,_= primitive.ObjectIDFromHex("60a3704aee35a58c9f6dc6b4")
		break
	case "2":
		user.Authority,_= primitive.ObjectIDFromHex("60a37054ee35a58c9f6dc6b5")
		break
	case "3":
		user.Authority,_= primitive.ObjectIDFromHex("60a37060ee35a58c9f6dc6b6")
		break
	case "4":
		user.Authority,_= primitive.ObjectIDFromHex("60a3706dee35a58c9f6dc6b7")
		break
	default:
		break
	}
	util.UpdateOne(c,"user",user,"_id",user.Id)
}


func UserPasswordUpdate(c *gin.Context,user model.User)  {
	//fmt.Println("user:", user)
	Collection := database.DB.Collection("user")
	salt := fmt.Sprintf("%d", rand.Int31())
	hash, err := bcrypt.GenerateFromPassword([]byte(user.Password+salt), bcrypt.DefaultCost) //加密处理
	encodePWD := string(hash)
	user.Password = encodePWD
	user.Salt = salt
	update := bson.M{"$set": user}
	updateResult, err := Collection.UpdateOne(context.Background(), bson.M{"_id":user.Id}, update)
	if err != nil {
		util.Return(c,200,util.EXCEPTION,err)
		log.Println(err)
		return
	}
	log.Println("collection.UpdateOne:", updateResult)
		util.Return(c,200, util.SUCCESS, user)
		return
}

